<?php
Yii::import('model.App');
Yii::import('lib.Util');
class AppController extends Controller {	
    public function actionIndex()
    {
    	$apps =  App::getInstance()->getOnlineApp(0);
    	$this->renderData['apps']= $apps;
    	$this->render('index',$this->renderData);
    }
    
    /**
     * 添加应用
     */
    public function actionAdd()
    {
    	$submit= Yii::app()->request->getParam('submit');
    	if ($submit)
    	{
			$data["app_name"] = trim(Yii::app ()->request->getParam ( 'app_name' ));
			$data["app_url"] = trim(Yii::app ()->request->getParam ( 'app_url' ));
			$data["app_order"] = trim(Yii::app ()->request->getParam ( 'app_order' ));
			$data["status"] = trim(Yii::app ()->request->getParam ( 'status' ));
			try {
				if ( App::getInstance()->addApp($data))
				{
					$this->exitWithSuccess("添加应用成功", '/app/index');
				}else{
					$this->exitWithError("添加应用失败", '/app/index');
				}
			} catch (Exception $e) {
				$this->exitWithError("添加应用失败", '/app/index');
			}
    		return;
    	}
    	$this->render('add');
    }
    
    /**
     * 修改应用
     */
    public function actionModify()
    {
    	$submit= Yii::app()->request->getParam('submit');
    	$appId = Yii::app()->request->getParam('appId');
    	if ($submit && $appId)
    	{
    		$data["app_name"] = trim(Yii::app ()->request->getParam ( 'app_name' ));
			$data["app_url"] = trim(Yii::app ()->request->getParam ( 'app_url' ));
			$data["app_order"] = trim(Yii::app ()->request->getParam ( 'app_order' ));
			$data["status"] = trim(Yii::app ()->request->getParam ( 'status' ));

    		if (App::getInstance()->modifyApp($data,$appId))
    		{    			
    			$this->exitWithSuccess("修改用户信息成功", '/app/index');
    		}else{
    			$this->exitWithError("修改用户信息失败", '/app/modify?appId='.$appId);
    		}
    		return;
    	}    	
    	
    	$app = App::getInstance()->getAppInfo($appId);
    	$this->renderData['app'] = $app;
    	$this->render('modify',$this->renderData);
    }
    
    
    public function actionDel()
    {
    	$appId  = Yii::app()->request->getParam('app_id');
    	if ($appId == 1)
    	{
    		$this->exitWithError("appID 为1的应用不能删除", '/app/index');
    	}
    	$ret = App::getInstance()->delApp($appId);
    	if ($ret)
    	{
    		$this->exitWithSuccess("删除应用成功", '/app/index');
    	}else {
    		$this->exitWithError("删除应用失败", '/app/index');
    	}
    }
}
?>